//
//  LFBIconView.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/9.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBIconView.h"
#import "LFBCustomButton.h"
#import "UIButton+WebCache.h"
#import "LFBIconModel.h"
#import "LFBWebViewController.h"
#import <UIImageView+WebCache.h>

@implementation LFBIconView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSLog(@"~~~~创建LFBIconView");
        
    }
    return self;
}

- (void)setupUI
{
    
}

- (void)setIconsArray:(NSArray *)iconsArray
{
    _iconsArray = iconsArray;
    
    //水平布局
    self.axis = UILayoutConstraintAxisHorizontal;
    //设置子视图填充方式
    self.distribution = UIStackViewDistributionFillEqually;
    
    for (LFBIconModel *model in iconsArray) {

        [self addChildButtonWithImageUrl:model.img andTitle:model.name andCustomURL:model.customURL];
    }
}

- (void)addChildButtonWithImageUrl:(NSString *)imageUrl andTitle:(NSString *)title andCustomURL:(NSString *)customURL
{
    LFBCustomButton *button = [LFBCustomButton new];
    [button setImage:[UIImage imageNamed:@"icon_icons_holder"] forState:UIControlStateNormal];
    [button setButtonImageWithUrl:imageUrl];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:17];
    button.backgroundColor = [UIColor whiteColor];
    button.customURL = customURL;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addArrangedSubview:button];
}

- (void)buttonClickAction:(LFBCustomButton *)button
{
//    NSLog(@"~~~~%@",button.customURL);
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LFBHomeIconButtonDidClickNotification object:self userInfo:@{KHomeIconButtonURL: button.customURL}];
}

@end
